package com.melon.entity.admin;



import com.melon.entity.BaseEntity;
import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by zhiqi.shao on 2017/3/15.
 */
@Data
@Entity
@Table(name = "u_user")
public class User  extends BaseEntity implements Serializable{

    private static final long serialVersionUID = 7491788231132010277L;

    @Id
    @Column(name = "uid")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long uid;

    @Column(name="username")
    private String username;

    @Column(name="email")
    private String email;

    @Column(name="password")
    private String password;

    @Transient
    private String repassword;

    @Column(name="phone")
    private String phone;

    @Column(name="last_login_time")
    private Date lastLoginTime;

    @Column(name="status")
    private int status;


    @ManyToMany
    @JoinTable(name = "u_user_role", joinColumns = {@JoinColumn(name = "uid")}, inverseJoinColumns = {@JoinColumn(name = "rid")})
    private List<Role> roleList;


    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", email='" + email + '\'' +
                ", password='" + password + '\'' +
                ", repassword='" + repassword + '\'' +
                ", phone='" + phone + '\'' +
                ", lastLoginTime=" + lastLoginTime +
                ", status=" + status +
                '}';
    }

    @Transient
    public Set<String> getRolesName() {
        List<Role> roles = getRoleList();
        Set<String> set = new HashSet<String>();
        for (Role role : roles) {
            set.add(role.getRname());
        }
        return set;
    }
}
